package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.citrus.cr.common.constant.OrderFeaturesKey;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.cz.base.tool.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.crb2btradecenter.domain.orderline.model.OrderLine;
import com.epoch.app.crb2btradecenter.enums.OrderStatus;
import com.epoch.app.crforecastsales.model.dto.ActivitySDO;
import com.epoch.app.mnordercenter.model.dto.OrderLineSDO;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @date 2023/5/25 19:52
 */
public class OrderLineToOrderLineSDOConverter {

    public static OrderLineSDO convert(OrderLine orderLine){
        OrderLineSDO orderLineSDO = new OrderLineSDO();
        orderLineSDO.setId(null != orderLine.getId() ? null : String.valueOf(orderLine.getId()));
        orderLineSDO.setOrderId(orderLine.getMainOrderId());
        orderLineSDO.setOrderBizId(orderLine.getMainOrderBizId());
//        orderLineSDO.setOrderLineNo(orderLine.getSerialNumber());
        orderLineSDO.setLineNum(orderLine.getSerialNumber());
        orderLineSDO.setOuterOrderLineId(orderLine.getOuterOrderLineId());
        orderLineSDO.setOrderLineTag(orderLine.getOrderLineTag());
        orderLineSDO.setProductGroupName(orderLine.getProductGroupName());
        orderLineSDO.setProductGroupCode(orderLine.getProductGroupCode());
        orderLineSDO.setScItemId(orderLine.getScItemId());
//        orderLineSDO.setScItemCode(orderLine.getScItemOutCode());
//        orderLineSDO.setScItemTitle(orderLine.getScItemTitle());
        orderLineSDO.setGoodsCode(orderLine.getScItemOutCode());
        orderLineSDO.setGoodsName(orderLine.getScItemTitle());
        if (null != orderLine.getStatus()) {
            orderLineSDO.setSubStatus(String.valueOf(orderLine.getStatus()));
            orderLineSDO.setSubStatusDesc(OrderStatus.of(orderLine.getStatus()).getDesc());
        }
        orderLineSDO.setSaleUnit(orderLine.getSaleUnit());
        orderLineSDO.setSaleUnitName(orderLine.getSaleUnitName());
        orderLineSDO.setInventoryUnit(orderLine.getInventoryUnitCode());
        orderLineSDO.setInventoryUnitName(orderLine.getInventoryUnit());
        orderLineSDO.setUnitConvert(orderLine.getUnitConvert());

        if (orderLine.getQuantity() != null) {
            orderLineSDO.setOrderQuantity(String.valueOf(orderLine.getQuantity()));
            orderLineSDO.setQuantity(String.valueOf(orderLine.getQuantity()));
        }
        orderLineSDO.setUnitPrice(orderLine.getUnitPrice());
        if (null != orderLine.getQuantity() && StringUtil.isNotEmpty(orderLine.getUnitPrice())) {
            BigDecimal a = new BigDecimal(orderLine.getQuantity());
            BigDecimal b = new BigDecimal(orderLine.getUnitPrice());
            orderLineSDO.setTotalAmount(String.valueOf(a.multiply(b)));
        }

        if (null != orderLine.getProductationTime()) {
            orderLineSDO.setProducteDate(DateUtils.toDateStr(orderLine.getProductationTime(),DateUtils.DATETIME_FORMAT));
        }

        orderLineSDO.setActivityUnitPrice(orderLine.getActivityPrice());
        orderLineSDO.setActivityTotalAmount(orderLine.getActualPaidFee());
        orderLineSDO.setShippedAmount(orderLine.getCumulativeReceiptQuantity());
        orderLineSDO.setDeliveryAmount(orderLine.getDeliveryAmount());
//        orderLineSDO.setRefuseReasonCode(orderLine.getRefuseReasonCode());
//        orderLineSDO.setRefuseReasonName(orderLine.getRefuseReasonName());
        orderLineSDO.setRejectReason(orderLine.getRefuseReasonCode());
        orderLineSDO.setClosed(orderLine.getClosed());
//        orderLineSDO.setOrderLineRemark(orderLine.getRemark());
        orderLineSDO.setRemark(orderLine.getRemark());

        orderLineSDO.setActivityId(orderLine.getActivityCode());
        orderLineSDO.setActivityLineNo(orderLine.getActivityTermId());
        //orderLineSDO.setActivityName(orderLine.getActivityName());
        orderLineSDO.setActivityDesc(orderLine.getActivityName());

        if (StringUtils.isNotBlank(orderLine.getFeatures())) {
            Map<String, String> features = JSONObject.parseObject(orderLine.getFeatures(), Map.class);
            if (MapUtils.isNotEmpty(features)) {
                if ("gift".equals(features.get("giftFlag"))||StringUtils.isBlank(features.get("giftFlag"))) {
                    //设置赠品标识
                    orderLineSDO.setIsGift(true);
                }
                //手工价格
                orderLineSDO.setManualPrice(StringUtils.isNotBlank(features.get(OrderFeaturesKey.MANUAL_PRICE)) ? features.get(OrderFeaturesKey.MANUAL_PRICE) : null);
                //手工总金额
                orderLineSDO.setManualTotalAmount(StringUtils.isNotBlank(features.get(OrderFeaturesKey.MANUAL_TOTAL_AMOUNT)) ? features.get(OrderFeaturesKey.MANUAL_TOTAL_AMOUNT) : null);

                List<ActivitySDO> activitySDOS = JSONObject.parseArray(features.get("activityDteails"), ActivitySDO.class);
                if(StringUtils.isBlank(orderLineSDO.getActivityLineNo()) && CollectionUtils.isNotEmpty(activitySDOS)){
                    List<String> list = activitySDOS.stream().map(ActivitySDO::getActivityTermid).collect(Collectors.toList());
                    String activityNumber = list.stream().collect(Collectors.joining("/"));
                    orderLineSDO.setActivityLineNo(activityNumber);
                }
            }

        }
        orderLineSDO.setSyncVersion(orderLine.getSyncVersion());
//        orderLineSDO.setCreatorId(orderLine.getCreatorId());
//        orderLineSDO.setCreatorName(orderLine.getCreatorName());
//        orderLineSDO.setUpdatorId();
//        orderLineSDO.setUpdatorName();
//        orderLineSDO.setGmtCreateDate();
        return orderLineSDO;
    }




}
